package com.srpingde.hhh;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.springde.hhh.R;
import com.srpingde.hhh.application.HhhApp;
import com.srpingde.hhh.application.HHHDataManage;
import com.srpingde.hhh.bean.HHHMeiTuDataBean;
import com.srpingde.hhh.bean.HHHMeiTuListBean;
import com.srpingde.hhh.bean.HHHMeiTuListDataBean;
import com.srpingde.hhh.http.AsyncHttpResponseHandler;
import com.srpingde.hhh.jiekou.HHHIml;
import com.srpingde.hhh.jiekou.HHHInterface;

public class HHHFenLeiXiangQingActivity extends Activity {
	//WeiQuTianDiMeiTuBean bean;
	//WeiQuFenLeiBean fenLeiBean;
	HHHMeiTuDataBean bean;
	HHHIml weiQuIml;
	String id = null;
	HHHMeiTuListBean gouWuChe;
	GridView gridView;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.activity_wi_qu_fen_lei_xiang_qing);
		setContentView(R.layout.weiqulistlayout);
		gridView = (GridView) findViewById(R.id.text1);
		weiQuIml = new HHHIml();
		if(getIntent().getExtras()!=null){
			bean = (HHHMeiTuDataBean) getIntent().getSerializableExtra("content");
			String url = HHHInterface.httpHeader + "api.php?tab=beautypiclist&act=data&pagesize=10&page=1&cid="+bean.getCid();
			 weiQuIml.weiQuTuPianList(url, HHHFenLeiXiangQingActivity.this, new AsyncHttpResponseHandler(){

				@Override
				public void onSuccess(String content) {
					// TODO Auto-generated method stub
					super.onSuccess(content);
					gouWuChe =HHHDataManage.getInstance().getWeiQuMeiTuListBean();
					gridView.setAdapter(new WeiquMeiTuAdapter(gouWuChe.getRows(), HHHFenLeiXiangQingActivity.this));
				}
			 });
			 gridView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					// TODO Auto-generated method stub
					Intent in = new Intent(HHHFenLeiXiangQingActivity.this,HHHMeiTuXiangQing.class);
					in.putExtra("image", gouWuChe.getRows().get(arg2).getSpic());
					in.putExtra("id", gouWuChe.getRows().get(arg2).getBid());
					startActivity(in);
				}
			});
//			final ImageView kvShow = show_iv;
//    		ImageLoader.getInstance().loadImage(WeiQuIml.httpHeader+bean.getBpic(), new SimpleImageLoadingListener(){
//    			@Override
//    			public void onLoadingComplete(String imageUri, View view, Bitmap bitmap) {
//    				//BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
//    				kvShow.setImageBitmap(bitmap);
//    				bigBitmap = bitmap;
//    				//kvShow.setBackgroundDrawable(bitmapDrawable);
//    			}
//		}
		}
	}
	public void onClick(View v){
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		}
	}
    
    class WeiquMeiTuAdapter extends BaseAdapter{
    	Context context;
    	ArrayList<HHHMeiTuListDataBean> list;
    	public WeiquMeiTuAdapter(ArrayList<HHHMeiTuListDataBean> list, 
    			Context context) {
    		super();
    		this.list = list;
    		this.context = context;
    	}
    	@Override
    	public int getCount() {
    		// TODO Auto-generated method stub
    		return list.size();
    	}

    	@Override
    	public Object getItem(int position) {
    		// TODO Auto-generated method stub
    		return null;
    	}

    	@Override
    	public long getItemId(int position) {
    		// TODO Auto-generated method stub
    		return 0;
    	}
    	private void itemLayout(ImageView imageView){
    		int wh = (int) (HhhApp.getInstance().getDisplayHightAndWightPx()[1]/2);
    		RelativeLayout.LayoutParams layoutParams = (LayoutParams) imageView.getLayoutParams();
    		layoutParams.height = wh;
    		layoutParams.width = wh;
    		imageView.setLayoutParams(layoutParams);
    	}
    	@Override
    	public View getView(int position, View convertView, ViewGroup parent) {
    		// TODO Auto-generated method stub
    		fenLeiHolder holder = null;
    		if(convertView == null){
    			holder = new fenLeiHolder();
    			convertView = LayoutInflater.from(context).inflate(R.layout.weiqulistbean, null);
    			holder.icon = (ImageView) convertView.findViewById(R.id.fenlei_item_icon);
    			convertView.setTag(holder);
    		}else{
    			holder = (fenLeiHolder) convertView.getTag();
    		}
    		itemLayout(holder.icon);
    		//holder.icon.setBackgroundResource(images[arg0]);
    		final ImageView kvShow = holder.icon;
    		ImageLoader.getInstance().loadImage(HHHIml.httpHeader+list.get(position).getSpic(), new SimpleImageLoadingListener(){
    			@Override
    			public void onLoadingComplete(String imageUri, View view, Bitmap bitmap) {
    				BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
    				//kvShow.setImageBitmap(bitmap);
    				kvShow.setBackgroundDrawable(bitmapDrawable);
    			}
    		});
    		return convertView;
    	}
    	class fenLeiHolder{
    		ImageView icon;
    	}
    }
	private void layout() {
		// 头的高度
		TextView top_title_tv = (TextView) findViewById(R.id.top_title);
		RelativeLayout.LayoutParams titleLayoutParams = (android.widget.RelativeLayout.LayoutParams) top_title_tv
				.getLayoutParams();
		titleLayoutParams.height = HhhApp.getInstance().title_h();
		top_title_tv.setLayoutParams(titleLayoutParams);
	}
}
